package com.project.smarthome.common.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.project.smarthome.R;


public class LoadingDialog extends Dialog {

    private LoadingDialog mDialog;

    public LoadingDialog(Context context, int theme) {
        super(context, theme);
    }

    public LoadingDialog(Context context) {
        super(context);

    }
    public void setMsg(String info){
        if(mDialog !=null){
            ((TextView) mDialog.getWindow().getDecorView().findViewById(R.id.txt_info)).setText(info);
        }
    }
    public LoadingDialog getDialog(Context context, String info) {
        dissMissDialog();
        Builder b = new Builder(context);
        b.setInfo(info);
        mDialog = b.create();
        mDialog.show();
        return mDialog;
    }


    public void dissMissDialog() {

        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    /**
     * Helper class for creating a custom mDialog
     */
    public static class Builder {
        private Context context;
        private TextView txtInfo;
        private String info;

        public void setInfo(String info) {
            this.info = info;
        }

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * Create the custom mDialog
         */
        public LoadingDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final LoadingDialog dialog = new LoadingDialog(context,
                    R.style.Dialog);
            dialog.setCanceledOnTouchOutside(false);
            View layout = inflater.inflate(R.layout.view_loading_dialog, null);
            txtInfo = (TextView) layout.findViewById(R.id.txt_info);
            txtInfo.setText(info);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);
            return dialog;
        }
    }

}